﻿/*

Copyright 2009 Guillaume Amringer

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

package {
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import flash.display.MovieClip;
	import events.InterfaceEvent;
	import events.MenuInterfaceEvent;
	import utils.Options;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;

	public class MenuInterface extends Interface {
		
		private var _assetsLoader:BulkLoader = new BulkLoader('menuInterfaceLoader');
		
		private var _tl:MovieClip = new MovieClip();
		private var _tr:MovieClip = new MovieClip();
		private var _bl:MovieClip = new MovieClip();
		private var _br:MovieClip = new MovieClip();
		private var _top:MovieClip = new MovieClip();
		private var _bottom:MovieClip = new MovieClip();
		private var _left:MovieClip = new MovieClip();
		private var _right:MovieClip = new MovieClip();
		private var _middle:MovieClip = new MovieClip();
		
		private var _selectedFilters:Array = new Array(new GlowFilter(0x24a3c8,.77,6,6,2,2));
		private var _menuSelectedTFormat:TextFormat = new TextFormat('Mangal', 16, 0xffffff);
		private var _menuIdleTFormat:TextFormat = new TextFormat('Mangal', 16, 0x9c9c9c);
		private var _SinglePlayerMC:MovieClip = new MovieClip();
		private var _SinglePlayerTF:TextField = new TextField();
		private var _MultiplayerMC:MovieClip = new MovieClip();
		private var _MultiplayerTF:TextField = new TextField();
		private var _EngageMC:MovieClip = new MovieClip();
		private var _EngageTF:TextField = new TextField();
		
		public function MenuInterface():void {
			
			_assetsLoader.add('interface/menu/tl.png',{id:'tl'});
			_assetsLoader.add('interface/menu/tr.png',{id:'tr'});
			_assetsLoader.add('interface/menu/bl.png',{id:'bl'});
			_assetsLoader.add('interface/menu/br.png',{id:'br'});
			_assetsLoader.add('interface/menu/top.png',{id:'top'});
			_assetsLoader.add('interface/menu/bottom.png',{id:'bottom'});
			_assetsLoader.add('interface/menu/left.png',{id:'left'});
			_assetsLoader.add('interface/menu/right.png',{id:'right'});
			_assetsLoader.add('interface/menu/middle.png', { id:'middle' } );
			
			//	Prepare TextFields
			_SinglePlayerTF.antiAliasType = AntiAliasType.ADVANCED;
			_SinglePlayerTF.autoSize = TextFieldAutoSize.LEFT;
			_SinglePlayerTF.selectable = false;
			_SinglePlayerTF.text = 'Single Player';
			_SinglePlayerTF.setTextFormat(_menuSelectedTFormat);
			_SinglePlayerMC.x = 20;
			_SinglePlayerMC.y = 20;
			_SinglePlayerMC.filters = _selectedFilters;
			_SinglePlayerMC.buttonMode = true;
			_SinglePlayerMC.smoothing = true;
			_SinglePlayerMC.mouseChildren = false;
			_SinglePlayerMC.addChild(_SinglePlayerTF);
			_SinglePlayerMC.addEventListener(MouseEvent.CLICK, _singlePlayerClickHandler);
			_middle.addChild(_SinglePlayerMC);
			
			_MultiplayerTF.antiAliasType = AntiAliasType.ADVANCED;
			_MultiplayerTF.autoSize = TextFieldAutoSize.LEFT;
			_MultiplayerTF.selectable = false;
			_MultiplayerTF.text = 'Multiplayer';
			_MultiplayerTF.setTextFormat(_menuIdleTFormat);
			_MultiplayerMC.x = 120;
			_MultiplayerMC.y = 20;
			_MultiplayerMC.buttonMode = true;
			_MultiplayerMC.smoothing = true;
			_MultiplayerMC.mouseChildren = false;
			_MultiplayerMC.addChild(_MultiplayerTF);
			_MultiplayerMC.addEventListener(MouseEvent.CLICK, _multiplayerClickHandler);
			_middle.addChild(_MultiplayerMC);
			
			_EngageTF.antiAliasType = AntiAliasType.ADVANCED;
			_EngageTF.autoSize = TextFieldAutoSize.LEFT;
			_EngageTF.selectable = false;
			_EngageTF.text = 'Engage';
			_EngageTF.setTextFormat(_menuIdleTFormat);
			_EngageMC.x = 200;
			_EngageMC.y = 150;
			_EngageMC.buttonMode = true;
			_EngageMC.smoothing = true;
			_EngageMC.mouseChildren = false;
			_EngageMC.addChild(_EngageTF);
			_EngageMC.addEventListener(MouseEvent.CLICK, _engageClickHandler);
			_middle.addChild(_EngageMC);
			
			_assetsLoader.addEventListener(BulkProgressEvent.COMPLETE, _assetsLoadingCompleteHandler);
		}
		
		public override function build():void {
			_assetsLoader.start();
		}
		
		public override function resize():void {
			
			//	Top bar
			_top.x = _tl.width;
			_top.y = 0;
			_top.width = globals.width - _tl.width - _tr.width;
			
			//	Bottom bar
			_bottom.x = _bl.width;
			_bottom.y = globals.height - _bottom.height;
			_bottom.width = globals.width - _bl.width - _br.width;
			
			//	Left bar
			_left.x = 0;
			_left.y = _tl.height;
			_left.height = globals.height - _tl.height - _bl.height;
			
			//	Right bar
			_right.x = globals.width - _right.width;
			_right.y = _tr.height;
			_right.height = globals.height - _tr.height - _br.height;
			
			//	Top left corner
			_tl.x = 0;
			_tl.y = 0;
			
			//	Top right corner
			_tr.x = globals.width - _tr.width;
			_tr.y = 0;
			
			//	Bottom left corner
			_bl.x = 0;
			_bl.y = globals.height - _bl.height;
			
			//	Bottom right corner
			_br.x = globals.width - _br.width;
			_br.y = globals.height - _br.height;
			
			//	Middle box
			_middle.x = globals.width / 2 - _middle.width / 2;
			_middle.y = globals.height / 2 - _middle.height / 2;
		}
		
		private function _assetsLoadingCompleteHandler(e:BulkProgressEvent):void {
			_tl.addChild(_assetsLoader.getBitmap('tl'));
			_tr.addChild(_assetsLoader.getBitmap('tr'));
			_bl.addChild(_assetsLoader.getBitmap('bl'));
			_br.addChild(_assetsLoader.getBitmap('br'));
			_top.addChild(_assetsLoader.getBitmap('top'));
			_bottom.addChild(_assetsLoader.getBitmap('bottom'));
			_left.addChild(_assetsLoader.getBitmap('left'));
			_right.addChild(_assetsLoader.getBitmap('right'));
			
			_middle.addChildAt(_assetsLoader.getBitmap('middle'),0);
			
			this._constructInterface();
		}
		
		private function _constructInterface():void {
			
			this.resize();
			this.addChild(_top);
			this.addChild(_bottom);
			this.addChild(_left);
			this.addChild(_right);
			this.addChild(_tl);
			this.addChild(_tr);
			this.addChild(_bl);
			this.addChild(_br);
			this.addChild(_middle);
			
			this.dispatchEvent(new InterfaceEvent(InterfaceEvent.READY));
		}
		
		private function _singlePlayerClickHandler(e:MouseEvent):void {
			_MultiplayerMC.filters = new Array();
			_MultiplayerTF.setTextFormat(_menuIdleTFormat);
			_SinglePlayerMC.filters = _selectedFilters;
			_SinglePlayerTF.setTextFormat(_menuSelectedTFormat);
			globals.options.mode = Options.SINGLE_PLAYER_MODE;
		}
		
		private function _multiplayerClickHandler(e:MouseEvent):void {
			_SinglePlayerMC.filters = new Array();
			_SinglePlayerTF.setTextFormat(_menuIdleTFormat);
			_MultiplayerMC.filters = _selectedFilters;
			_MultiplayerTF.setTextFormat(_menuSelectedTFormat);
			globals.options.mode = Options.MULTI_PLAYER_MODE;
		}
		
		private function _engageClickHandler(e:MouseEvent):void {
			this.dispatchEvent(new MenuInterfaceEvent(MenuInterfaceEvent.ENGAGE_CLICK));
		}
	}
}